package com.soft.mainsite.component.beetl.tag.content;

import cn.hutool.core.convert.Convert;
import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.common.exception.SystemException;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.module.web.cms.service.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * ContentSelectTag.java 此类用于多选选择生成
 *
 * @author:李昭阳
 * @date:2019/5/28 0028
 * @remark:
 */
@Slf4j
@Service
@Scope("prototype")
public class ContentSelectTag extends GeneralVarTagBinding {

    @Autowired
    private ContentService contentService;

    @Value("${system.site.subfix}")
    private String siteSubfix;

    @Value("${system.site.prefix}")
    private String sitePrefix;

    private static final String CASE_INTELLIGENT_SYSTEM = "case_intelligent_system";
    private static final String SOLUTION_INTELLIGENT_SYSTEM = "solution_intelligent_system";
    private static final String CASE_PRODUCT_LIST = "case_product_list";
    private static final String SOLUTION_PRODUCT_LIST = "solution_product_list";


    /**
     * 文章列表标签
     * <p>
     * categoryId:分类编号
     * siteType:站点类型
     */
    @Override
    public void render() {

        if (CmsUtil.isNullOrEmpty(this.args[1])) {
            throw new SystemException("标签参数不能为空！");
        }
        String siteType = Convert.toStr(this.getAttributeValue("siteType"));
        Long modelId = Convert.toLong(this.getAttributeValue("modelId"));
        Long contentId = Convert.toLong(this.getAttributeValue("contentId"));

        if (CmsConst.MAIN_SITE_TYPE.equals(siteType)) {
            siteType = null;
        }
        List<Map> byModelIdAndSiteType = contentService.findByModelIdAndSiteType(modelId, siteType);

        try {
            this.wrapRender(byModelIdAndSiteType, contentId, modelId);
        } catch (Exception e) {
            log.error("推荐栏目渲染失败", e);
        }

    }

    private void wrapRender(List<Map> contentList, Long contentId, Long modelId) {
        String selectIds = "";
        String[] selectIdArray = new String[]{};
        if (contentId != null) {
            selectIds = getSelectIds(contentId, modelId);
        }
        int i = 1;
        if (StringUtils.isNotBlank(selectIds)) {
            selectIdArray = selectIds.split(",");
        }
        for (Map content : contentList) {
            if (ArrayUtils.isNotEmpty(selectIdArray) && ArrayUtils.contains(selectIdArray, content.get("content_id") == null ? "" : content.get("content_id").toString())) {
                content.put("checked", "checked");
            } else {
                content.put("checked", "");
            }

            content.put("index", i);
            content.put("count", contentList.size());
            this.binds(content);
            this.doBodyRender();
            i++;
        }
    }

    private String getSelectIds(Long contentId, Long modelId) {
        String selectIds = "";
        Map byContentIdAndModelId = contentService.findByContentId(contentId);
        if (CmsConst.INTELLIGENT_SUBSYSTEM_MODEL_ID.equals(modelId)) {
            // 获取动态表字段的数据（智慧化系统）
            if (byContentIdAndModelId.containsKey(CASE_INTELLIGENT_SYSTEM) && StringUtils.isNotBlank(byContentIdAndModelId.get(CASE_INTELLIGENT_SYSTEM).toString())) {
                selectIds = byContentIdAndModelId.get(CASE_INTELLIGENT_SYSTEM).toString();
            }
            if (byContentIdAndModelId.containsKey(SOLUTION_INTELLIGENT_SYSTEM) && StringUtils.isNotBlank(byContentIdAndModelId.get(SOLUTION_INTELLIGENT_SYSTEM).toString())) {
                selectIds = byContentIdAndModelId.get(SOLUTION_INTELLIGENT_SYSTEM).toString();
            }
        }
        if (CmsConst.HOME_SETTINGS_MODEL_ID.equals(modelId)) {
            // 获取动态表字段的数据（产品清单）
            if (byContentIdAndModelId.containsKey(CASE_PRODUCT_LIST) && StringUtils.isNotBlank(byContentIdAndModelId.get(CASE_PRODUCT_LIST).toString())) {
                selectIds = byContentIdAndModelId.get(CASE_PRODUCT_LIST).toString();
            }
            if (byContentIdAndModelId.containsKey(SOLUTION_PRODUCT_LIST) && StringUtils.isNotBlank(byContentIdAndModelId.get(SOLUTION_PRODUCT_LIST).toString())) {
                selectIds = byContentIdAndModelId.get(SOLUTION_PRODUCT_LIST).toString();
            }
        }
        return selectIds;
    }

}
